import api from '@/store/api';

export interface powerSupply {
  id: string;
  health: string;
  partNumber: number;
  serialNumber: string;
  efficiencyPercent: string;
  firmwareVersion: string;
  identifyLed: string;
  manufacturer: string;
  model: string;
  powerInputWatts: number;
  name: string;
  sparePartNumber: string;
  locationNumber: string;
  statusState: string;
}

export const usePowerSupplyStore = defineStore('PowerSupplyStore', () => {
  const powerSupplies = ref([] as powerSupply[]);
  const setPowerSupply = (data: any[]) => {
    powerSupplies.value = data.map(
      (powerSupply: {
        EfficiencyPercent: any;
        FirmwareVersion: any;
        LocationIndicatorActive: any;
        MemberId: any;
        Manufacturer: any;
        Model: any;
        Name: any;
        PartNumber: any;
        PowerInputWatts: any;
        SerialNumber: any;
        SparePartNumber: any;
        Location: any;
        Status?: { [index: string]: string } | undefined;
      }) => {
        const {
          EfficiencyPercent,
          FirmwareVersion,
          LocationIndicatorActive,
          MemberId,
          Manufacturer,
          Model,
          Name,
          PartNumber,
          PowerInputWatts,
          SerialNumber,
          SparePartNumber,
          Location,
          Status = {},
        } = powerSupply;
        return {
          id: MemberId,
          health: Status.Health,
          partNumber: PartNumber,
          serialNumber: SerialNumber,
          efficiencyPercent: EfficiencyPercent,
          firmwareVersion: FirmwareVersion,
          identifyLed: LocationIndicatorActive,
          manufacturer: Manufacturer,
          model: Model,
          powerInputWatts: PowerInputWatts,
          name: Name,
          sparePartNumber: SparePartNumber,
          locationNumber: Location?.PartLocation?.ServiceLabel,
          statusState: Status.State,
        };
      },
    );
  };
  const getChassisCollection = async () => {
    return await api
      .get('/redfish/v1/Chassis')
      .then(({ data: { Members } }) =>
        Members.map((member: { [x: string]: any }) => member['@odata.id']),
      )
      .catch((error) => console.log(error));
  };
  const getAllPowerSupplies = async () => {
    const collection = await getChassisCollection();
    if (!collection) return;
    return await api
      .all(collection.map((chassis: any) => getChassisPower(chassis)))
      .then((supplies: any) => {
        let suppliesList: any[] = [];
        supplies.forEach(
          (supply: any) => (suppliesList = [...suppliesList, ...supply]),
        );
        setPowerSupply(suppliesList);
      })
      .catch((error: any) => console.log(error));
  };
  const getChassisPower = async (id: string) => {
    return await api
      .get(`${id}/Power`)
      .then(({ data: { PowerSupplies } }) => PowerSupplies || [])
      .catch((error) => console.log(error));
  };

  return {
    powerSupplies,
    setPowerSupply,
    getChassisCollection,
    getAllPowerSupplies,
    getChassisPower,
  };
});
